/*
 * Copyright (C) 2013 'Chaos'.Personal
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.silly.test;

import java.util.ArrayList;

import android.app.Activity;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ListView;

import com.silly.adapter.AdapterBox;
import com.silly.adapter.EntityAdapter;
import com.silly.adapter.R;
import com.silly.adapter.entity.SillyBean;
import com.silly.adapter.utils.RUtils;

public class MainActivtiy extends Activity implements OnClickListener {

	public static final int SILLY_ADAPTER = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		ListView list = (ListView) findViewById(R.id.list);
		
        long time = ts();		
        buildAdapter(25);
        System.out.println(ts() - time);
        list.setAdapter(AdapterBox.find(SILLY_ADAPTER));
	}
	
	public long ts(){
		return System.currentTimeMillis();
	}

	private void buildAdapter(int count) {
		ArrayList<SillyBean> list = new ArrayList<SillyBean>(count);
		for (int i = 0; i < count; i++) {
            if(i % 2 == 0){
            	list.add(buildPersonalInfo(i));
            }else{
            	list.add(buildImageWeave());
            }
		}
        AdapterBox.build(getApplicationContext(), list, SILLY_ADAPTER);
	}
	
	private SillyBean buildPersonalInfo(int pos){
		PersonalInformation infomation = new PersonalInformation();
        infomation.listener = this;
        infomation.tag = pos;
		return infomation;
	}
	
	private SillyBean buildImageWeave(){
		ImageWeave weave = new ImageWeave();
        weave.weave_right = BitmapFactory.decodeResource(getResources(), R.drawable.weave);
		return weave;
	}

	@Override
	public void onClick(View v) {
        EntityAdapter adapter = AdapterBox.find(SILLY_ADAPTER);
        
        int pos = (Integer) v.getTag();
        PersonalInformation pi = (PersonalInformation) adapter.getItem(pos);
        
        //更改进度条进度
        if(pi.progress < 100){
        	//增加进度
        	pi.progress += 20;
        }else{
        	RUtils.showTip(getApplicationContext(), "进度达到Max");
        }
        
        //refresh
        adapter.refresh(pos);
        //you can do this
//        adapter.refresh(pi);
	}
}
